home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 November / Chip Kasım 2003.iso / prog / openoff / f_0155 / DBMeta.xba next >
Encoding:
Extensible Markup Language  |  2003-02-17  |  9.4 KB  |  327 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="DBMeta" script:language="StarBasic">REM  *****  BASIC  *****
  4. Option Explicit
  5.  
  6.  
  7. Public iCommandTypes() as Integer
  8. Public CurCommandType as Integer
  9. Public oDataSource as Object
  10. Public bEnableBinaryOptionGroup as Boolean
  11.  
  12.  
  13. Function GetDatabaseNames(baddFirstListItem as Boolean)
  14. Dim sDatabaseList()
  15.     If oDBContext.HasElements Then
  16.         Dim LocDBList() as String
  17.         Dim MaxIndex as Integer
  18.         Dim i as Integer
  19.         LocDBList = oDBContext.ElementNames()
  20.         MaxIndex = Ubound(LocDBList())
  21.         If baddfirstListItem Then
  22.             ReDim Preserve sDatabaseList(MaxIndex + 1)
  23.             sDatabaseList(0) = sSelectDatasource
  24.             a = 1
  25.         Else
  26.             ReDim Preserve sDatabaseList(MaxIndex)
  27.             a = 0
  28.         End If
  29.         For i = 0 To MaxIndex
  30.             sDatabaseList(a) = oDBContext.ElementNames(i)
  31.             a = a + 1
  32.         Next i
  33.     End If
  34.     GetDatabaseNames() = sDatabaseList()
  35. End Function
  36.  
  37.  
  38. Sub GetSelectedDBMetaData()
  39. Dim OldsDBname as String
  40. Dim DBIndex as Integer
  41. Dim LocList() as String
  42.     If Ubound(DialogModel.lstDatabases.SelectedItems()) > -1 Then
  43.         DeleteFirstListBoxEntry("lstDatabases", sSelectDatasource)
  44.         ToggleDatabasePage(False)
  45.         DBIndex = DialogModel.lstDatabases.SelectedItems(0)
  46.         With DialogModel
  47.             If DBIndex > -1 Then
  48.                 sDBName = DlgFormDB.getControl("lstDatabases").getSelectedItem()
  49.                 If GetConnection(sDBName) Then
  50.                     If GetDBMetaData() Then
  51.                         LocList() = AddListToList(Array(sSelectDBTable), TableNames())
  52.                         .lstTables.StringItemList() = AddListToList(LocList(), QueryNames())
  53.                         .lstTables.SelectedItems() = Array(0)
  54.                         iCommandTypes() = CreateCommandTypeList()
  55.                         EmptyFieldsListboxes()
  56.                     End If
  57.                 End If
  58.                 bEnableBinaryOptionGroup = False
  59.                 .lstTables.Enabled = True
  60.                 .lblTables.Enabled = True
  61.             Else
  62.                 DialogModel.lstTables.StringItemList = Array(sSelectDBTable)
  63.                 EmptyFieldsListboxes()
  64.             End If
  65.             ToggleDatabasePage(True)
  66.         End With
  67.     End If
  68. End Sub
  69.  
  70.  
  71. Function GetConnection(sDBName as String)
  72. Dim oInteractionHandler as Object
  73. Dim bExitLoop as Boolean
  74. Dim bGetConnection as Boolean
  75. Dim iMsg as Integer
  76. Dim Nulllist()
  77.     If Not IsNull(oDBConnection) Then
  78.         oDBConnection.Dispose()
  79.     End If
  80.     oDataSource = oDBContext.GetByName(sDBName)
  81.     If Not oDBContext.hasbyName(sDBName) Then
  82.         GetConnection() = False
  83.         Exit Function
  84.     End If
  85.     If Not oDataSource.IsPasswordRequired Then
  86.         oDBConnection = oDBContext.GetByName(sDBName).GetConnection("","")
  87.         GetConnection() = True
  88.     Else
  89.         oInteractionHandler = createUnoService("com.sun.star.sdb.InteractionHandler")
  90.         oDataSource = oDBContext.GetByName(sDBName)
  91.         On Local Error Goto NOCONNECTION
  92.         Do
  93.             bExitLoop = True
  94.             oDBConnection = oDataSource.ConnectWithCompletion(oInteractionHandler)
  95.             NOCONNECTION:
  96.             bGetConnection = Err = 0
  97.             If bGetConnection Then
  98.                 bGetConnection = Not IsNull(oDBConnection)
  99.                 If Not bGetConnection Then
  100.                     Exit Do
  101.                 End If
  102.             End If
  103.             If Not bGetConnection Then
  104.                 iMsg = Msgbox (sMsgNoConnection,32 + 2, sMsgWizardName)
  105.                 bExitLoop = iMsg = SBCANCEL
  106.                 Resume CLERROR
  107.                 CLERROR:
  108.             End If
  109.         Loop Until bExitLoop
  110.         On Local Error Goto 0
  111.         If Not bGetConnection Then
  112.             DialogModel.lstDatabases.SelectedItems() = Array(sSelectDatasource)
  113.             DialogModel.lstTables.StringItemList() = Array(sSelectDBTable)
  114.             DialogModel.lstFields.StringItemList() = NullList()
  115.             DialogModel.lstSelFields.StringItemList() = NullList()
  116.         End If
  117.         GetConnection() = bGetConnection
  118.     End If
  119. End Function
  120.  
  121.  
  122. Function GetDBMetaData()
  123.     If oDBContext.HasElements Then
  124.         Tablenames() = oDBConnection.Tables.ElementNames()
  125.         Querynames() = oDBConnection.Queries.ElementNames()
  126.         GetDBMetaData = True
  127.     Else
  128.         MsgBox(sMsgErrNoDatabase, 64, sMsgWizardName)
  129.         GetDBMetaData = False
  130.     End If
  131. End Function
  132.  
  133.  
  134. Sub GetTableMetaData()
  135. Dim iType as Long
  136. Dim m as Integer
  137. Dim Found as Boolean
  138. Dim i as Integer
  139. Dim sFieldName as String
  140. Dim n as Integer
  141. Dim WidthIndex as Integer
  142. Dim oField as Object
  143.     MaxIndex = Ubound(DialogModel.lstSelFields.StringItemList())
  144.     Dim ColumnMap(MaxIndex)as Integer
  145.     FieldNames() = DialogModel.lstSelFields.StringItemList()
  146.     ' Build a structure which maps the position of a selected field (within the selection) to the the column position within
  147.     ' the table. So we ensure that the controls are placed in the same order the according fields are selected.
  148.     For i = 0 To Ubound(FieldNames())
  149.         sFieldName = FieldNames(i)
  150.         Found = False
  151.         n = 0
  152.         While (n< MaxIndex And (Not Found))
  153.             If (FieldNames(n) = sFieldName) Then
  154.                 Found = True
  155.                 ColumnMap(n) = i
  156.             End If
  157.             n = n + 1
  158.         Wend
  159.     Next i
  160.     For n = 0 to MaxIndex
  161.         sFieldname = FieldNames(n)
  162.         oField = oColumns.GetByName(sFieldName)
  163.         iType = oField.Type
  164.         FieldMetaValues(n,0) = oField.Type
  165.         FieldMetaValues(n,1) = AssignFieldLength(oField.Precision)
  166.         FieldMetaValues(n,2) = GetValueoutofList(iType, WidthList(),1, WidthIndex)
  167.         FieldMetaValues(n,3) = WidthList(WidthIndex,3)
  168.         FieldMetaValues(n,4) = oField.FormatKey
  169.         FieldMetaValues(n,5) = oField.DefaultValue
  170.         FieldMetaValues(n,6) = oField.IsCurrency
  171.         FieldMetaValues(n,7) = oField.Scale
  172. '        If oField.Description <> "" Then
  173. '' Todo: What's wrong with this line?
  174. '            Msgbox oField.Helptext
  175. '        End If
  176.         FieldMetaValues(n,8) = oField.Description
  177.     Next
  178.     ReDim oDBShapeList(MaxIndex) as Object
  179.     ReDim oTCShapeList(MaxIndex) as Object
  180.     ReDim oDBModelList(MaxIndex) as Object
  181.     ReDim oGroupShapeList(MaxIndex) as Object
  182. End Sub
  183.  
  184.  
  185. Function GetSpecificFieldNames() as Integer
  186. Dim n as Integer
  187. Dim m as Integer
  188. Dim s as Integer
  189. Dim iType as Integer
  190. Dim oField as Object
  191. Dim MaxIndex as Integer
  192. Dim EmptyList()
  193.     If Ubound(DialogModel.lstTables.StringItemList()) > -1 Then
  194.         FieldNames() = oColumns.GetElementNames()
  195.         MaxIndex = Ubound(FieldNames())
  196.         If MaxIndex <> -1 Then
  197.             Dim ResultFieldNames(MaxIndex)
  198.             ReDim ImgFieldNames(MaxIndex)
  199.             m = 0
  200.             For n = 0 To MaxIndex
  201.                 oField = oColumns.GetByName(FieldNames(n))
  202.                 iType = oField.Type
  203.                 If GetIndexInMultiArray(WidthList(), iType, 0) <> -1 Then
  204.                     ResultFieldNames(m) = FieldNames(n)
  205.                     m = m + 1
  206.                 End If
  207.                 If GetIndexInMultiArray(ImgWidthList(), iType, 0) <> -1 Then
  208.                     ImgFieldNames(s) = FieldNames(n)
  209.                     s = s + 1
  210.                 End If
  211.             Next n
  212.             If s <> 0 Then
  213.                 Redim Preserve ImgFieldNames(s-1)
  214.                 bEnableBinaryOptionGroup = True
  215.             Else
  216.                 bEnableBinaryOptionGroup = False
  217.             End If
  218.             Redim Preserve ResultFieldNames(m-1)
  219.             Redim Preserve FieldNames(m-1)
  220.             FieldNames() = ResultFieldNames()
  221.             DialogModel.lstFields.StringItemList = FieldNames()
  222.             InitializeListboxProcedures(DialogModel, DialogModel.lstFields, DialogModel.lstSelFields)
  223.         End If
  224.         GetSpecificFieldNames = MaxIndex
  225.     Else
  226.         GetSpecificFieldNames = -1
  227.     End If
  228. End Function
  229.  
  230.  
  231. Sub CreateDBForm()
  232.     If oDrawPage.Forms.Count = 0 Then
  233.           oDBForm = oDocument.CreateInstance("com.sun.star.form.component.Form")
  234.         oDrawpage.Forms.InsertByIndex (0, oDBForm)
  235.     Else
  236.         oDBForm = oDrawPage.Forms.GetByIndex(0)
  237.     End If
  238.     oDBForm.Name = "Standard"
  239.     oDBForm.DataSourceName = sDBName
  240.     oDBForm.Command = TableName
  241.     oDBForm.CommandType = CurCommandType
  242. End Sub
  243.  
  244.  
  245. Sub AddOrRemoveBinaryFieldsToWidthList()
  246. Dim LocWidthList()
  247. Dim MaxIndex as Integer
  248. Dim OldMaxIndex as Integer
  249. Dim s as Integer
  250. Dim n as Integer
  251. Dim m as Integer
  252.     If Not bDebug Then
  253.         On Local Error GoTo WIZARDERROR
  254.     End If
  255.     If DialogModel.optBinariesasGraphics.State = 1 Then
  256.         OldMaxIndex = Ubound(WidthList(),1)
  257.         If OldMaxIndex = 15 Then
  258.             MaxIndex = Ubound(WidthList(),1) + Ubound(ImgWidthList(),1) + 1
  259.             ReDim Preserve WidthList(MaxIndex,4)
  260.             s = 0
  261.             For n = OldMaxIndex + 1 To MaxIndex
  262.                 For m = 0 To 3
  263.                     WidthList(n,m) = ImgWidthList(s,m)
  264.                 Next m
  265.                 s = s + 1
  266.             Next n
  267.             MergeList(DialogModel.lstFields, ImgFieldNames())
  268.         End If
  269.     Else
  270.         ReDim Preserve WidthList(15, 4)
  271.         RemoveListItems(DialogModel.lstFields(), DialogModel.lstSelFields(), ImgFieldNames())
  272.     End If
  273.     DialogModel.lstSelFields.Tag = True
  274. WIZARDERROR:
  275.     If Err <> 0 Then
  276.         Msgbox(sMsgErrMsg, 16, GetProductName())
  277.         Resume LOCERROR
  278.         LOCERROR:
  279.     End If
  280. End Sub
  281.  
  282.  
  283. Function CreateCommandTypeList()
  284. Dim MaxTableIndex as Integer
  285. Dim MaxQueryIndex as Integer
  286. Dim MaxIndex as Integer
  287. Dim i as Integer
  288. Dim a as Integer
  289.     MaxTableIndex = Ubound(TableNames()
  290.     MaxQueryIndex = Ubound(QueryNames()
  291.     MaxIndex = MaxTableIndex + MaxQueryIndex + 1
  292.     If MaxIndex > -1 Then
  293.         Dim LocCommandTypes(MaxIndex) as Integer
  294.         For i = 0 To MaxTableIndex
  295.             LocCommandTypes(i) = com.sun.star.sdb.CommandType.TABLE
  296.         Next i
  297.         a = i
  298.         For i = 0 To MaxQueryIndex
  299.             LocCommandTypes(a) = com.sun.star.sdb.CommandType.QUERY
  300.             a = a + 1
  301.         Next i
  302.     End If
  303.     CreateCommandTypeList() = LocCommandTypes()
  304. End Function
  305.  
  306.  
  307. Sub GetCurrentMetaValues(Index as Integer)
  308.     CurFieldType = FieldMetaValues(Index,0)
  309.     CurFieldLength = FieldMetaValues(Index,1)
  310.     CurControlType = FieldMetaValues(Index,2)
  311.     CurControlName = FieldMetaValues(Index,3)
  312.     CurFormatKey = FieldMetaValues(Index,4)
  313.     CurDefaultValue = FieldMetaValues(Index,5)
  314.     CurIsCurrency = FieldMetaValues(Index,6)
  315.     CurScale = FieldMetaValues(Index,7)
  316.     CurHelpText = FieldMetaValues(Index,8)
  317.     CurFieldName = FieldNames(Index)
  318. End Sub
  319.  
  320.  
  321. Function AssignFieldLength(FieldLength as Long) as Integer
  322.     If FieldLength >= 65535 Then
  323.         AssignFieldLength() = -1
  324.     Else
  325.         AssignFieldLength() = FieldLength
  326.     End If
  327. End Function</script:module>